package com.wang.head.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wang.common.controller.BaseController;
import com.wang.common.pojo.ApiResponse;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.pojo.ABodyStand;
import com.wang.users.pojo.ABodyside;
import com.wang.users.pojo.AUsers;
import com.wang.users.service.IABodysideService;

/**
 * 体测前端
 * 
 * @author MyJjh
 *
 */
@Controller
@RequestMapping("/head/bodys")
public class BodyHeadController extends BaseController {
	@Autowired
	private IABodysideService bodysideService;

	/**
	 * 体侧历史列表页面
	 * 
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/sidesList")
	public String sidesList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ConstatFinalUtil.SYS_LOGGER.info("--sidesList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<ABodyside> apiResponse = this.bodysideService.findCondListService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());

		return "/head/bodys/sidesList";
	}

	/**
	 * 打开添加体侧历史页面
	 * 
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/sidesInsert")
	public String sidesInsert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ConstatFinalUtil.SYS_LOGGER.info("--sidesInsert--");
		Map<String, Object> condMap = new HashMap<String, Object>();
		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<ABodyStand> standApiResponse = this.bodysideService.findCondListBodyStandService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("standResponse", standApiResponse.toJSON());
		return "/head/bodys/sidesInsert";
	}

	/**
	 * 添加体侧历史提交操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "sidesInsertSubmit")
	public String sidesInsertSubmit(HttpServletRequest request, HttpServletResponse response, ABodyside sides,
			String email) throws ServletException, IOException {
		ConstatFinalUtil.SYS_LOGGER.info("--sidesInsertSubmit--");
		HttpSession session = request.getSession();
		AUsers users = (AUsers) session.getAttribute("usersSess");
		int usersId = users.getId();
		sides.setUsersId(usersId);
		/* 默认是 未审核的 */
		sides.setStatus(Byte.valueOf("0"));
		/* 保存结果 */
		ApiResponse<Object> dbResponse = this.bodysideService.saveOneBodysideService(sides);
		ConstatFinalUtil.SYS_LOGGER.info("结果:{}", dbResponse.toJSON().toJSONString());

		request.setAttribute("response", dbResponse);
		return this.sidesInsert(request, response);
	}

	/**
	 * 打开更新体侧历史页面
	 * 
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/sidesUpdate")
	public String sidesUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// operType=update&idType=session
		ConstatFinalUtil.SYS_LOGGER.info("--sidesUpdate--");
		String id = request.getParameter("id");
		String operType = request.getParameter("operType");

		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<ABodyside> apiResponse = this.bodysideService.findOneBodysideService(condMap);
		/* 存储request */
		request.setAttribute("response", apiResponse.toJSON());
		request.setAttribute("operType", operType);

		if ("update".equalsIgnoreCase(operType)) {
			condMap.clear();
			/*
			 * 查询数据库 所有的数据以JSON的形式返回
			 */
			ApiResponse<ABodyStand> standApiResponse = this.bodysideService.findCondListBodyStandService(null, condMap);
			/* 将结果存储到Request中 */
			request.setAttribute("standResponse", standApiResponse.toJSON());
			
			return "/head/bodys/sidesUpdateInfo";
		} else if ("stands".equalsIgnoreCase(operType)) {
			return "/head/bodys/standsInfo";
		} else if ("users".equalsIgnoreCase(operType)) {
			return "/head/bodys/usersInfo";
		}
		return "/head/bodys/sidesInfo";
	}

	/**
	 * 更新体侧历史提交操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "sidesUpdateSubmit")
	public String sidesUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ConstatFinalUtil.SYS_LOGGER.info("--sidesUpdateSubmit--");
		/* 数据库操作 */
		HttpSession session = request.getSession();
		ApiResponse dbResponse = new ApiResponse<>();
		String id = request.getParameter("id");
		String operType = request.getParameter("operType");

		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<ABodyside> apiResponse = this.bodysideService.findOneBodysideService(condMap);
		/* 获取java对象 */
		ABodyside sides = apiResponse.getDataOneJava();
		boolean flag = false;
		if ("update".equalsIgnoreCase(operType)) {
			/* 接收参数 */
			String standId = request.getParameter("standId");
			String vitalcapacity = request.getParameter("vitalcapacity");
			String height = request.getParameter("height");
			String weight = request.getParameter("weight");

			sides.setStandId(Integer.valueOf(standId));
			sides.setVitalcapacity(Integer.valueOf(vitalcapacity));
			sides.setHeight(Double.valueOf(height));
			sides.setWeight(Double.valueOf(weight));
			flag = true;
		}
		dbResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(dbResponse.getCode() + ""), Collections.EMPTY_MAP);
		if (flag) {
			dbResponse = bodysideService.updateOneBodysideService(sides);
		}
		ConstatFinalUtil.SYS_LOGGER.info("--用户更新返回结果:{}--", dbResponse.toJSON());
		request.setAttribute("response", dbResponse);
		return this.sidesUpdate(request, response);
	}

	/**
	 * 体侧标准列表页面
	 * 
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/standsList")
	public String standsList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ConstatFinalUtil.SYS_LOGGER.info("--standsList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<ABodyStand> apiResponse = this.bodysideService.findCondListBodyStandService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());

		return "/head/bodys/standsList";
	}

	/**
	 * 打开添加体侧标准页面
	 * 
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/standsInsert")
	public String standsInsert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ConstatFinalUtil.SYS_LOGGER.info("--standsInsert--");
		Map<String, Object> condMap = new HashMap<String, Object>();
		return "/head/bodys/standsInsert";
	}

	/**
	 * 添加体侧标准提交操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "standsInsertSubmit", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String standsInsertSubmit(HttpServletRequest request, HttpServletResponse response, ABodyStand stands)
			throws ServletException, IOException {
		ConstatFinalUtil.SYS_LOGGER.info("--standsInsert--");

		/* 保存结果 */
		ApiResponse<Object> dbResponse = this.bodysideService.saveOneBodyStandService(stands);
		ConstatFinalUtil.SYS_LOGGER.info("结果:{}", dbResponse.toJSON().toJSONString());
		String result = dbResponse.toJSON().toJSONString();

		return result;
	}

	/**
	 * 打开更新体侧标准页面
	 * 
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/standsUpdate")
	public String standsUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// operType=update&idType=session
		ConstatFinalUtil.SYS_LOGGER.info("--standsUpdate--");
		String id = request.getParameter("id");
		String operType = request.getParameter("operType");

		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<ABodyStand> apiResponse = this.bodysideService.findOneBodyStandService(condMap);
		/* 存储request */
		request.setAttribute("response", apiResponse.toJSON());
		request.setAttribute("operType", operType);

		if ("update".equalsIgnoreCase(operType)) {
			return "/head/bodys/standsUpdateInfo";
		}
		return "/head/bodys/standsInfo";
	}

	/**
	 * 更新体侧标准提交操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "standsUpdateSubmit", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String standsUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ConstatFinalUtil.SYS_LOGGER.info("--standsUpdateSubmit--");
		/* 数据库操作 */
		HttpSession session = request.getSession();
		ApiResponse dbResponse = new ApiResponse<>();
		String id = request.getParameter("id");
		String operType = request.getParameter("operType");

		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<ABodyStand> apiResponse = this.bodysideService.findOneBodyStandService(condMap);
		/* 获取java对象 */
		ABodyStand stands = apiResponse.getDataOneJava();
		boolean flag = false;
		if ("update".equalsIgnoreCase(operType)) {
			/* 接收参数 */
			String grade = request.getParameter("grade");
			String vitalcapacity = request.getParameter("vitalcapacity");
			String longrun = request.getParameter("longrun");
			String shortrun = request.getParameter("shortrun");
			String abdominalcurl = request.getParameter("abdominalcurl");
			String sittingforward = request.getParameter("sittingforward");
			String height = request.getParameter("height");
			String weight = request.getParameter("weight");
			String bml = request.getParameter("bml");
			String sex = request.getParameter("sex");
			String status = request.getParameter("status");

			stands.setGrade(Integer.valueOf(grade));
			stands.setVitalcapacity(Integer.valueOf(vitalcapacity));
			stands.setLongrun(Double.valueOf(longrun));
			stands.setShortrun(Double.valueOf(shortrun));
			stands.setAbdominalcurl(Integer.valueOf(abdominalcurl));
			stands.setSittingforward(Double.valueOf(sittingforward));
			stands.setHeight(Double.valueOf(height));
			stands.setWeight(Double.valueOf(weight));
			stands.setBml(Double.valueOf(bml));
			stands.setSex(Byte.valueOf(sex));
			stands.setStatus(Byte.valueOf(status));
			flag = true;
		}
		dbResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(dbResponse.getCode() + ""), Collections.EMPTY_MAP);
		if (flag) {
			dbResponse = bodysideService.updateOneBodyStandService(stands);
		}
		ConstatFinalUtil.SYS_LOGGER.info("--用户更新返回结果:{}--", dbResponse.toJSON());
		String result = dbResponse.toJSON().toJSONString();
		return result;
	}
	
}
